﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bamuca.Codigo.DAO;
using Bamuca.Codigo.DTO;

namespace Bamuca.Views
{
    public partial class Default : System.Web.UI.Page
    {
        UsuarioDao _usuarioDao = new UsuarioDao();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Convert.ToString(Request.QueryString["logout"])))
            {
                Session.Clear();
                SessaoDto.IdUsuario = 0;
                SessaoDto.Login = string.Empty;
                SessaoDto.Nome = string.Empty;
            }
            else
            {
                if (!string.IsNullOrEmpty(Convert.ToString(Session["login"])))
                {
                    Response.Redirect("Home.aspx");
                }
            }
            
            tbLogin.Focus();
            
        }

        protected void btLogar_Click(object sender, EventArgs e)
        {
            if (_usuarioDao.Logar(tbLogin.Text, tbSenha.Text))
            {
                Session["idusuario"] = SessaoDto.IdUsuario;
                Session["login"] = SessaoDto.Login;
                Session["nome"] = SessaoDto.Nome;

                Response.Redirect("Home.aspx");
            }
            else
            {
                lbErro.Text = "Usuário ou senha inválidas!! Refaça o login.";
                ScriptManager.RegisterStartupScript(this, GetType(), "", "javascript:$('#divErro').show(1500);", true);
            }
        }
    }
}